import https from 'https';
import URL from 'url'
import path from "path";
import { fileURLToPath } from "url";

export const sleep = (interval) => {
    return new Promise(resolve => {
        setTimeout(resolve, interval);
    })
}


export const ajax = {
    get(url) {
        return new Promise((resolve) => {
            const option = {
                ...URL.parse(url),
                rejectUnauthorized: false, // 忽略https安全性
                method: 'GET',        // 请求方式
                headers: {
                    referer: url,    // 如果资源有防盗链，则清空该属性
                },
            }
            https.get(option, res => {
                // res.setEncoding('binary');
                let list = [];
                res.on('data', chunk => {
                    list.push(chunk);
                });
                res.on('end', () => {
                    const res = Buffer.concat(list);
                    console.log(list);
                    resolve(res)
                });
            })
        })

    }

}

export const getPath = () => {
    const __dirname = path.dirname(fileURLToPath(import.meta.url));
    const rootPath = path.join(__dirname, "../../");
    return {
        __dirname,
        rootPath,
        path
    }
}